home *** CD-ROM | disk | FTP | other *** search
| Text File | 1989-05-02 | 6.7 KB | 251 lines | [TEXT/MPS ] |
- % ==================================================
- % File: Processes
- %
- % This Example demonstrate how you can dynamically create
- % windows and processes to control them. Notice how in this
- % case multiple instances of the same class "Controller"
- % are designed to handle only events to its own Window.
- % The Windows are of different kinds and behaves
- % differently due to the virtual procedure "Draw".
- %
- % Notice also that different objects of the same class
- % "CreatItem" are used in the create menu. They distinguish
- % their actions by using the parameters to the virtual
- % procedure "doMenu".
- %
- % The macProcessMGR schedules the process instances and
- % does all the book keeping before activating them.
- %
- % Compile: simcomp processes
- % Link: simld processes -toolbox -APPL
- % This sequence can be generated by entering "make processes"
- %
- % Using: create process with the "create" menu.
- % Click them to make them draw. See how they
- % are updated when overlapping, moved and resized
- % =====================================================
- begin
- external class macRect="::sinterfaces:macRect";
- external class macProcessmgr="::sinterfaces:macprocessmgr";
- external class macProcess="::sinterfaces:macprocess";
- external class macmenubar="::sinterfaces:macmenubar";
- external class defaultmenuprocess="::sinterfaces:defaultmenuprocess";
- external class macmenu="::sinterfaces:macmenu";
- external class macmenuitem="::sinterfaces:macmenuitem";
- external class defaultAppleMenu="::Sinterfaces:defaultAppleMenu";
-
- !--------------------------------------------;
- ! Windows
- !--------------------------------------------;
- ! Here MacWindow is used as super class to several
- ! specializations. They differ by different
- ! implementations of the virtual procedure "Draw".
- ! The sub class of macProcess: "Controller" need
- ! only know about "RandomWindow":s
- !---------------------------;
- macwindow class randomwindow(seed,Width,Height);integer Seed,Width,Height;
- virtual: procedure draw is
- procedure draw;;
- begin
- integer Count; ! Updated by "Draw" each time it is called;
- integer InitialSeed;
- ! ReDraw is called when the window has to be updated ;
- procedure ReDraw;
- begin
- integer i,todo;
- seed:=InitialSeed;
- todo:=count;
- count:=0;
- for i:=1 step 1 until todo do
- Draw;
- end;
- initialseed:=seed;
- end;
-
- RandomWindow class RandomDraw;
- begin
- procedure Draw;
- begin
- integer dx,dy,i;
- PenNormal;
- moveto(Width/2,Height/2);
- for i:=1 step 1 until 10 do
- begin
- dx:=randint(0,20,seed)-10;
- dy:=randint(0,40,seed)-20;
- line(dx,dy);
- end;
- Count:=Count+1;
- end --- Draw ---;
- end --- Randow Draw --- ;
-
- randomWindow class RandomText;
- begin
- procedure Draw;
- begin
- integer x,y,s,f,i,ch;
- PenNormal;
- for i:=1 step 1 until 10 do
- begin
- x:=randint(0,Width,Seed)-5;
- y:=randint(0,Height,Seed)-10;
- ch:=randint(rank('a'),rank('z'),Seed);
- moveto(x,y);
- drawchar(char(ch));
- end;
- Count:=Count+1;
- end --- Draw ---;
-
- end --- Randow Text --- ;
-
- randomWindow class RandomFont;
- begin
- procedure Draw;
- begin
- integer x,y,s,f,i,ch;
- PenNormal;
- s:=randint(9,20,Seed);
- f:=randint(0,24,Seed);
- textfont(f);
- textsize(s);
- for i:=1 step 1 until 10 do
- begin
- x:=randint(0,Width,Seed)-5;
- y:=randint(0,Height,Seed)-10;
- ch:=randint(rank('a'),rank('z'),Seed);
- moveto(x,y);
- drawchar(char(ch));
- end;
- Count:=Count+1;
- end --- Draw ---;
- end --- Randow Font --- ;
- !--------------------------------------------;
- ! Process
- !--------------------------------------------;
- ! Controller: is designed to control any
- ! subclass of class randomWindow.
- !---------------------------;
- macProcess class Controller(W); ref(randomWindow) W;
- begin
- ! Called when the User have touched the "res-size"
- ! button.
- !---------------;
- procedure doSize(width,height); integer width,height;
- begin
- W.SizeWindow(width,height,true);
- end;
- ! Called when the user have clicked the "goAway" button.
- !-----------------;
- procedure doGoAway;
- begin
- W.closewindow;
- theMGR.kill(this controller);
- end;
- ! --- main loop of the process -- ;
- ref(macEvent) E;
- integer i;
- E:- new macEvent;
- EnableEvent(Tconst.mouseDown);
- EnableEvent(TConst.updateEvt);
- while true do
- begin
- waitnextevent(e);
- if e.what=Tconst.updateEvt then
- begin
- W.beginUpdate;
- W.reDraw;
- W.EndUpdate;
- end
- else ! - Mouse down event - ;
- W.Draw;
- end;
- end --- Controller --;
- !--------------------------------------------;
- ! Menu classes
- !--------------------------------------------;
- ! Almost the same thing should be done
- ! when any of the window-types are created
- ! We therefore define one CreateItem class
- ! that checks the "i" parameter to create a
- ! Window of correct type. An alternative
- ! is to define one Item-class for each
- ! alternative.
- !---------------------------;
- macmenuItem class CreateItem;
- begin
- procedure doMenu(m,i); integer m,i;
- begin
- ref(randomWindow) w1;
- ref(macrect) Wbounds;
- text Title;
- if i=1 then
- begin
- w1:- new RandomDraw(GlobalSeed,200,200);
- Title:-"Random Drawing";
- end
- else if i=2 then
- begin
- w1:- new RandomText(GlobalSeed,200,200);
- Title:-"Random Text";
- end
- else
- begin
- w1:- new RandomFont(GlobalSeed,200,200);
- Title:-"Random Font";
- disableItem; ! -- one Font-Window is enough -;
- end;
- Wbounds:-new macRect;
- inspect Wbounds do
- begin
- top:=Randint(50,250,GlobalSeed); bottom:=top+W1.Width;
- left:=Randint(50,250,GlobalSeed); right:=left+W1.Height;
- end;
- W1.NewWindow(Wbounds,Title,true,0,none,true,0);
- theMGR.RegisterWindow(w1);
- theMGR.RegisterProcess(new Controller(w1),w1);
- end;
- end --- Create Item --- ;
-
- macmenuitem class quitItem;
- begin
- procedure doMenu(m,i); integer m,i;
- theMGR.stop;
- end --- quitItem --- ;
-
- ref(macProcessMgr) theMGR;
- ref(macmenubar) menubar;
- ref(macmenu) menu1,menu2;
- ref(macprocess) p;
- integer Globalseed;
-
- Globalseed:=1147;
- ! -- One each of these classes -- ;
- theMGR:- new macprocessmgr;
- menubar:- new macmenubar;
- P:- new defaultmenuprocess(Menubar);
- theMGR.RegisterMenuProcess(P);
-
- ! -- Set up the menu -- ;
- menubar.insertmenu(new defaultApplemenu,0);
-
- Menu2:-new MacMenu;
- Menu2.newmenu(133,"File");
- menubar.insertmenu(Menu2,0);
- Menu2.appendmenu("quit/Q");
- menu2.registeritem(new QuitItem,1);
- Menu1:-new macMenu;
- Menu1.newMenu(132,"Create");
- menubar.insertmenu(Menu1,0);
- Menu1.appendmenu("Draw/D");
- Menu1.appendmenu("Text");
- Menu1.appendmenu("Font");
- Menu1.RegisterItem(new CreateItem,1);
- Menu1.RegisterItem(new CreateItem,2);
- Menu1.RegisterItem(new CreateItem,3);
- Menubar.DrawmenuBar;
- ! -- Let the Event scheduler take control -- ;
- theMGR.run;
- end
-
-
-